<?php

/**
 * 作者：lucas
 * 链接：
 * 邮箱：dissipator_520@qq.com
 */

/**
 * NodeAction
 *
 * @author lucas
 */
class CustomAction extends BaseAction {

    public function index() {
        $this->display();
    }

    public function finish() {
        $this->display();
    }
    
    public function getData() {
        $Mode = D('CustomView');
        $where=$this->condition;
        if(!$where)$where['status']=array('EQ','P');
                // p($where);die;
        $data = $Mode->where($where)->page($this->page, $this->rows)->order($this->order)->select();
        Log::write($Mode->getLastSql());
        $this->returnGridData($data, $Mode->where($this->condition)->count());
    }

    public function toAdd() {

        $fields=array('PO','邮件时间','需求品种','需求数量','开始时间','截止时间','实际发货数量','status','备注','c_id');
        foreach ($fields as $key => $value) {
                    switch ($value)
                    {
                    case 'c_id':
                        $output .= '
                        <tr>
                            <td>' .$value .':</td>
                            <td valign="middle" colspan="3">
                            <select id="cc" name="' .$value .'" style="width:150px;"></select>
                            </td>
                        </tr>';
                    break; 
                    case '邮件时间':
                        $output .= '<tr><td>' .$value .'：</td><td><input name="'.$value.'"class="easyui-datebox"  required="required" type="text" value="" style="width: 150px;"/></td></tr>';
                    break; 
                    case '开始时间':
                        $output .= '<tr><td>' .$value .'：</td><td><input name="'.$value.'"class="easyui-datebox"  required="required" type="text" value="" style="width: 150px;"/></td></tr>';
                    break;

                    case '截止时间':
                        $output .= '<tr><td>' .$value .'：</td><td><input name="'.$value.'"class="easyui-datebox"  required="required" type="text" value="" style="width: 150px;"/></td></tr>';
                    break;
                    default:
                        $output .= '
                        <tr>
                            <td>' .$value .'：</td>
                            <td><input name="'.$value.'" required="required" type="text" value="" style="width: 150px;"/></td>
                        </tr>';
                    }

            $data['output']= $output;
            $data['c_id']= 1;
            // echo  $output;
            $this->assign('data',$data);
        }
        $this->display('add');
    }

    public function doSave() {

        $Model = M("khask");
        $data=$_POST;
        // p($data);
        if ($data['ID']){
            $Model -> data($data) -> where('ID=%d',$data['ID']) -> save();
            Log::write($Model->getLastSql());
            $result = true;
        }else{ 
                if (!$Model->create()) {
                    $this->returnStatus(false, $Model->getError());
                    Log::write($Model->getLastSql());
                } else {
                    $Model->startTrans();
                    $result = false;
                    if (empty($Model->ID)) {
                        $result = $Model->data($data)->add();
                        Log::write($Model->getLastSql());
                        // p($data);
                    }
        }}       
            $Model->commit();
            $this->returnStatus();
    }

    public function doDelete() {
        $ids = $_POST['ids'];
        $array = explode(',', $ids);
        // p($array);
        if ($array && count($array) > 0) {
            $Model = M("khask");
            foreach ($array as $id) {
                $Model->where('ID = %d', $id)->delete();
            }
        }
        $this->returnStatus();
    }

    public function toUpdate() {

        $ID = $_GET['ID'];
        // p($_GET);die;
        $fields=array('PO','ID','邮件时间','需求品种','需求数量','开始时间','截止时间','实际发货数量','status','备注','c_id');
        if ($ID) {
            $Model = M("khinf");
            $khinf = $Model->field('ID,saname,name')->select();
            $this->assign('khinf', json_encode($khinf));
            $Model = M('khask');
            $data = $Model->where('ID = %d', $ID)->field($fields)->find();
            // p($data);die;
            if ($data) {
                foreach ($data as $key => $value) {
                    switch ($key)
                    {
                    case 'ID':
                        $output .= '<input name="ID" required="required" type="hidden" value="' .$value. '" style="width: 150px;"/>';

                      break;  
                    case 'c_id':
                        $output .= '
                        <tr>
                            <td>' .$key .':</td>
                            <td valign="middle" colspan="3">
                            <select id="cc" name="' .$key .'" style="width:150px;"></select>
                            </td>
                        </tr>';
                        break;
                    case '开始时间':
                        $output .= '<tr><td>' .$key .'：</td><td><input name="'.$key.'"class="easyui-datebox"  value="' .$value. '"  value="" style="width: 150px;"/></td></tr>';
                    break;

                    case '截止时间':
                        $output .= '<tr><td>' .$key .'：</td><td><input name="'.$key.'"class="easyui-datebox"  value="' .$value. '"  value="" style="width: 150px;"/></td></tr>';
                    break;  
                    default:
                    $output .= '<tr>
                                      <td>' .$key .'：</td><td><input name="'.$key.'" required="required" type="text" value="' .$value. '" style="width: 150px;"/></td>
                                </tr>';
                    }
                    $data['output']=$output ;
                }
                $this->assign('data', $data);
            }
            else 
            $this->assign('data', '无效ID号请检查！');
        }
        $this->display('add');
    }


//客户信息表的处理
    public function infor() {
        $this->display();
    }

    public function getinfData() {
        $id = $_POST['c_id'];
        $Model = D('CustomView');
        $data = $Model->getCustominfData($id);
        // p($data);
        $this->returnGridData($data[0],$data[1]);
    }

    public function tocfAdd() {

        $fields=array('saname','name','城市E','地址1','国家','邮编','FedEx帐号','联系人','电话');
        foreach ($fields as $key => $value) {
                $output .= '<tr><td>' .$value .'：</td><td><input name="'.$value.'" required="required" type="text" value="" style="width: 150px;"/></td></tr>';
        }
        $data['output']= $output;
        $this->assign('data',$data);
        $this->display('infadd');
    }

    public function tocfUpdate() {
        $ID = $_GET['ID'];
        $fields=array('saname','name','城市E','地址1','国家','邮编','FedEx帐号','联系人','电话','ID');
        if ($ID) {
            $Model = M('khinf');
            $data = $Model->where('ID = %d', $ID)->field($fields)->find();
            Log::write($Model->getLastSql());
            // p($data);
            // echo $ID;
            if ($data) {
                foreach ($data as $key => $value) {
                    switch ($key)
                    {
                    case 'ID':
                        $output .= '<input name="ID" required="required" type="hidden" value="' .$value. '" style="width: 150px;"/>';

                      break;  
                    case 'label2':
                      $output .= '<tr><td>' .$key .'：</td><td><input name="'.$key.'" required="required" type="text" value="' .$value. '" style="width: 150px;"/></td></tr>';
                      break;
                    default:
                    $output .= '<tr><td>' .$key .'：</td><td><input name="'.$key.'" required="required" type="text" value="' .$value. '" style="width: 150px;"/></td></tr>';
                    }
                    $data['output']=$output ;
                }
                $this->assign('data', $data);
            }
            else 
            $this->assign('data', '无效ID号请检查！');
        }
        $this->display('infadd');
    }


    public function docfSave() {

        $Model = M("khinf");
        $data=$_POST;
        // p($data);
        if ($data['ID']){
            $Model -> data($data) -> where('ID=%d',$data['ID']) -> save();
            Log::write($Model->getLastSql());
            $result = true;
        }else{ 
                if (!$Model->create()) {
                    $this->returnStatus(false, $Model->getError());
                    Log::write($Model->getLastSql());
                } else {
                    $Model->startTrans();
                    $result = false;
                    if (empty($Model->ID)) {
                        $result = $Model->data($data)->add();
                        Log::write($Model->getLastSql());
                        // p($data);
                    }
        }}       
            $Model->commit();
            $this->returnStatus();
    }

    public function docfDelete() {
        $ids = $_POST['ids'];
        $array = explode(',', $ids);
        // p($array);
        if ($array && count($array) > 0) {
            $Model = M("khinf");
            foreach ($array as $id) {
                $Model->where('ID = %d', $id)->delete();
            }
        }
        $this->returnStatus();
    }

    /**
     * 获取combo数据
     */
    public function comboData($id='') {

        $Model = M("khinf");
        if($id){
            // return 1;
        $data = $Model->where('ID=%d',$id)->Order('saname')->select();
        $counts= $Model->where('ID=%d',$id)->count();
        }
        else{
            // return 2;
            $data = $Model->Order('saname')->select();
            $counts= $Model->count();
        }
        $this -> returnGridData($data,$counts);
        // $this->ajaxReturn(genTree($data,'id','name'));
    }

}

?>